#!./common/bats/bin/bats
# -*-sh-*-

load ./common/test_helper_functions || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

check_rms
rm=$RESOURCE_MANAGER

NODES=2
TASKS=8
ARGS=8
MAX_THREADS=`tau_exec ./get_max_threads`
LAST_THREAD_INDEX=`echo "$MAX_THREADS -1" | bc`
LAST_TASK_INDEX=`echo "$TASKS -1" | bc`

export TAU_TRACE=0 TAU_CALLPATH=0 TAU_PROFILE=1 TAU_METRICS=GET_TIME_OF_DAY
unset OMP_NUM_THREADS


@test "[libs/TAU] MPI C binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s C_mpi_test ];then
    flunk "C_mpi_test binary does not exist"
    fi

    run_mpi_binary "tau_exec ./C_mpi_test" "" $NODES $TASKS
    assert_success

    run ls profile.$LAST_TASK_INDEX.0.0
    assert_success

    rm -rf profile*
}

@test "[libs/TAU] MPI C++ binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ "x$ARCH" == "xx86_64" ];then
    TAU_METRICS=PAPI_LD_INS
    fi
    if [ ! -s CXX_mpi_test ];then
    flunk "CXX_mpi_test binary does not exist"
    fi

    run_mpi_binary ./run_CXX_mpi_test.sh $ARGS $NODES $TASKS
    assert_success

    ls profile.$LAST_TASK_INDEX.0.0
    assert_success

    rm -rf profile*
}

@test "[libs/TAU] Serial C OpenMP binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ "x$ARCH" == "xx86_64" ];then
    TAU_METRICS=PAPI_L1_DCM
    fi
    if [ ! -s C_omp_test ];then
    flunk "C_omp_test binary does not exist"
    fi

    run_serial_binary ./run_C_omp_test.sh
    assert_success

    ls profile.0.0.$LAST_THREAD_INDEX
    assert_success

    rm -rf profile*
}

@test "[libs/TAU] Serial FORTRAN binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    export TAU_TRACE=1 TAU_PROFILE=0
    if [ ! -s F_test ];then
    flunk "F_test binary does not exist"
    fi

    run_serial_binary "tau_exec ./F_test"
    assert_success

    ls tautrace.0.0.0.trc
    assert_success

    rm -f events.0.edf
    rm -f tautrace.0.0.0.trc
}
